home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / make / icmake-6.000 / icmake-6 / icmake / rss / getvar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  2.8 KB  |  91 lines

  1. /*
  2. \funcref{getvar}{UNS16 getvar (\params)}
  3.     {
  4.         {FILE} {*f} {binary makefile to read from}
  5.         {BIN\_HEADER\_} {*headerp} {pointer to header information}
  6.         {VAR\_} {**var} {address of pointer to {\em VAR\_} structs}
  7.     }
  8.     {number of read variables, or --1 upon failure}
  9.     {xrealloc(), error(), initvar()}
  10.     {getstring(), readheader()}
  11.     {getvar.c}
  12.     {
  13.         Function {\em getvar()} attempts to read the variables defined in a
  14.         binary makefile. Argument {\em headerp} is expected to point to a {\em
  15.         BIN\_HEADER\_} struct filled with header information.
  16.  
  17.         {\em var} is the address of a pointer to {\em VAR\_} structures. {\em
  18.         getvar()} reallocates the pointer as necessary; therefore, {\em $*$var}
  19.         must point to allocated memory or must be {\em NULL}. For each created
  20.         variable of the type {\bf list} or {\bf string}, the {\em vu.i} field
  21.         is set to {\em NULL} to reflect that the variable is not (yet) in use.
  22.         The {\em count} field is set to 1, reflecting one user of the attached
  23.         memory block.
  24.  
  25.         When no error occurs, {\em getvar()} returns the number of read
  26.         variables and restores the file pointer {\em f} to the location prior
  27.         to reading. When an error occurs, {\em --1} is returned and the file
  28.         pointer is not repositioned.
  29.     }
  30.  
  31. Example:
  32. {\footnotesize
  33.     \begin{verbatim}
  34.         // 'f' is assumed to be the opened file,
  35.         UNS16
  36.             nvar,
  37.             i;
  38.         VAR_
  39.             *var = NULL;
  40.         BIN_HEADER_
  41.             *headerp;
  42.  
  43.         headerp = readheader (f);
  44.         if ( (nvar = getvar (f, headerp, &var)) == -1 )
  45.             error ("cannot get variables from binary makefile");
  46.         for (i = 0; i < nvar; i++)
  47.         {
  48.             printf ("Variable %d is a ", i);
  49.             if (var [i].type & e_str)
  50.                 puts ("string");
  51.             else if (var [i].type & e_int)
  52.                 puts ("integer);
  53.             .
  54.             .. etcetera
  55.             .
  56.         }
  57.     \end{verbatim}
  58. } % end footnotesize
  59. */
  60.  
  61. #include "icrssdef.h"
  62.  
  63. UNS16 getvar (FILE *f, BIN_HEADER_ *headerp, VAR_ **var)
  64. {
  65.     register
  66.         nvar = 0;
  67.     INT32
  68.         curoffs;
  69.  
  70.     if (headerp->offset[1] == headerp->offset[2])
  71.         return (0);
  72.  
  73.     curoffs = ftell (f);
  74.     if (fseek (f, headerp->offset[1], SEEK_SET))
  75.         return ( (UNS16) -1);
  76.  
  77.     while (ftell (f) < headerp->offset[2])
  78.     {
  79.         *var = xrealloc (*var, (nvar + 1) * sizeof (VAR_));
  80.         if (! fread (*var + nvar, sizeof (VAR_), 1, f) )
  81.             error ("cannot read in variables");
  82.         if ((*var) [nvar].type > e_list)
  83.             error ("bad variable type (var #%d)\n", nvar + 1);
  84.         (*var)[nvar] = initvar ((*var)[nvar]);
  85.         nvar++;
  86.     }
  87.  
  88.     fseek (f, curoffs, SEEK_SET);
  89.     return (nvar);
  90. }
  91.